package handler

import (
	"context"
	"gozero-microservices/gateway-api/internal/logic"
	"gozero-microservices/gateway-api/utils"
	"gozero-microservices/user-service/rpc/types/user"
	"net/http"
	"strconv"
)

/*
 +----------------------------------------------------------------------
 + Title        : UserHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2019-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 用户处理器
 +----------------------------------------------------------------------
*/
type UserHandler struct {
	BaseHandler
}

/**
 * 注册
 *
 * @param string username -------------------------必传
 * @param string mobile ---------------------------必传
 * @param string password -------------------------必传
 * @author huwl
 */
func (control *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
	username := r.PostFormValue("username")
	mobile := r.PostFormValue("mobile")
	password := r.PostFormValue("password")

	if username == "" {
		control.BaseError(r, w, control.WithMessage("请输入用户名"))
		return
	}
	if !utils.CheckMobile(mobile) {
		control.BaseError(r, w, control.WithMessage("请输入正确格式的手机号"))
		return
	}
	if password == "" {
		control.BaseError(r, w, control.WithMessage("请输入密码"))
		return
	}

	// 调用逻辑层
	userLogic := logic.NewUserLogicInstance(context.Background(), control.SvcCtx)
	err := userLogic.Register(map[string]interface{}{
		"username": username,
		"mobile":   mobile,
		"password": password,
	})
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("注册成功"))
}

/**
 * 用户名登录
 *
 * @param string username -------------------------必传
 * @param string password -------------------------必传
 * @author huwl
 */
func (control *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
	username := r.PostFormValue("username")
	password := r.PostFormValue("password")

	if username == "" {
		control.BaseError(r, w, control.WithMessage("请输入用户名"))
		return
	}
	if password == "" {
		control.BaseError(r, w, control.WithMessage("请输入密码"))
		return
	}

	// 调用逻辑层
	userLogic := logic.NewUserLogicInstance(context.Background(), control.SvcCtx)
	result, err := userLogic.Login(map[string]interface{}{
		"username": username,
		"password": password,
	})
	if err != nil {
		control.BaseError(r, w, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(r, w, control.WithData(result))
}

/**
 * 列表
 *
 * @author huwl
 */
func (control *UserHandler) List(w http.ResponseWriter, r *http.Request) {
	uid, _ := strconv.Atoi(utils.DefaultQuery(r, "uid", "0"))
	name := utils.DefaultQuery(r, "name")
	nick := utils.DefaultQuery(r, "nick")
	email := utils.DefaultQuery(r, "email")
	username := utils.DefaultQuery(r, "username")
	startCreateTime := utils.DefaultQuery(r, "start_create_time")
	endCreateTime := utils.DefaultQuery(r, "end_create_time")
	status := utils.DefaultQuery(r, "status", "all")
	page, _ := strconv.Atoi(r.URL.Query().Get("page"))
	pageSize, _ := strconv.Atoi(r.URL.Query().Get("page_size"))
	sortField := r.URL.Query().Get("sort_field")
	sortMode := r.URL.Query().Get("sort_mode")

	params := &user.QueryUserRequest{
		Uid:             uint64(uid),
		Name:            name,
		Nick:            nick,
		Email:           email,
		Username:        username,
		Status:          status,
		StartCreateTime: startCreateTime,
		EndCreateTime:   endCreateTime,
	}

	if page == 0 {
		page = 1
	}
	if pageSize == 0 {
		pageSize = 10
	}
	if sortField == "" {
		sortField = "uid"
	}
	if sortMode == "" {
		sortMode = "DESC"
	}

	// 调用逻辑层
	userLogic := logic.NewUserLogicInstance(context.Background(), control.SvcCtx)
	userRes, err := userLogic.List(params, uint32(page), int8(pageSize), sortField+" "+sortMode)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"page":       page,
		"page_size":  pageSize,
		"total":      userRes.Total,
		"page_total": userRes.PageTotal,
		"users":      userRes.Users,
	}))
}
